package objectio;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Queue;

class Consumer implements Runnable {
	private Queue<Person> q;
	private ObjectOutputStream objectOutputStream;
	public Consumer(Queue<Person> q,ObjectOutputStream objectOutputStream) {
		super();
		this.q = q;
		this.objectOutputStream = objectOutputStream;
	}

	@Override
	public void run() {
		System.out.println("comsum start.........");
		while(true){
			synchronized (q) {
				
				if (q.isEmpty()) {
					try {
						q.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Person x = q.remove();
				try {
					objectOutputStream.writeObject(x);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				System.out.println("comsum......" + q.size());
/*				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}*/
				q.notify();
				if(Main.flag)
					break;
			}
		}
	}
}
